<%@ Assembly Name="Microsoft.SharePoint.ApplicationPages, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%> <%@ Page Language="C#" Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage,Microsoft.SharePoint,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" MasterPageFile="~/_layouts/application.master"   EnableViewState="false" EnableViewStateMac="false" %> <%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %> <%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Import Namespace="Microsoft.SharePoint" %>
<asp:Content ID="Content1" ContentPlaceHolderId="PlaceHolderPageTitle" runat="server">
    <SharePoint:EncodedLiteral ID="EncodedLiteral1" runat="server" text="Clear Lists" EncodeMethod='HtmlEncode'/>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderId="PlaceHolderPageTitleInTitleArea" runat="server">
    <SharePoint:EncodedLiteral ID="EncodedLiteral2" runat="server" text="Clear Lists" EncodeMethod='HtmlEncode'/>
</asp:Content>
<asp:Content ID="Content8" ContentPlaceHolderId="PlaceHolderMain" runat="server">
<%
    this.Response.Buffer = false;
    
    string strSourceUrl = this.Request["Source"];
    string strListUrl = this.Request["ListUrl"];
    string strListGuid = this.Request["List"];

    if (strSourceUrl == null && (strListGuid == null || strListUrl == null))
    {
        this.Response.Write("This page requires a 'Source' parameter that contains the Url of the list to clear and either a 'List' parameter that points to a list to clear in the current site or a 'ListUrl' paremeter that points to the full list path to clear.");
        this.Response.Flush();
    }
    else
    {
        this.Response.Write("The SourceUrl is " + strSourceUrl + ".<BR>We will redirect there after the list has been cleared.<BR>");
        this.Response.Flush();
        
        SPWeb currentWeb = null;
        SPList listToClear = null;

        if (strListUrl != null && strListGuid != null)
        {
            this.Response.Write("ListUrl is " + strListUrl + "<BR>");
            this.Response.Write("List is " + strListGuid + "<BR>");
            this.Response.Flush();

            currentWeb = new SPSite(strListUrl).OpenWeb();
            listToClear = currentWeb.Lists[new Guid(strListGuid)];
        }
        else if (strListUrl != null)
        {
            this.Response.Write("ListUrl is " + strListUrl + "<BR>");
            this.Response.Flush();

            currentWeb = new SPSite(strListUrl).OpenWeb();
            listToClear = currentWeb.GetListFromUrl(strListUrl);
        }
        else // (strListGuid != null)
        {
            this.Response.Write("ListGuid is " + (new Guid(strListGuid)).ToString() + "<BR>");
            this.Response.Flush();

            currentWeb = SPContext.Current.Web;
            listToClear = currentWeb.Lists[new Guid(strListGuid)];
        }

        this.Response.Write("Got the list " + listToClear.Title + "<BR>");
        this.Response.Flush();
        
        //clear 50 items at a time
        currentWeb.AllowUnsafeUpdates = true;
        int intRemainingItems = BlackBlade.ListActions.ClearList.ClearListAction.ClearList(listToClear, 50);

        //do some cleanup
        currentWeb.Dispose();
        currentWeb = null;

        if (intRemainingItems > 0)
        {
            this.Response.Write("There are still " + intRemainingItems + " items left to delete.<BR>Redirecting to: <a href='" + this.Request.Url.ToString() + "'>" + this.Request.Url.ToString() + "</a><BR>");
            this.Response.Flush();

            //there are still items left to delete. redirect back to this page
            %>
            <script type="text/javascript">
                window.setTimeout("window.location = window.location", 3000);
            </script>
            <%
        }
        else
        {
            this.Response.Write("There are no items left to delete.<BR>Redirecting to: <a href='" + strSourceUrl + "' >" + strSourceUrl + "</a><BR>");
            this.Response.Flush();

            //there are no items left to delete. go back to the source.
            %>
            <script type="text/javascript">
                window.setTimeout("window.location = '<%=strSourceUrl %>'", 3000);
            </script>
            <%
        }
    }
%>
</asp:Content>
